package com.twu.biblioteca;


import java.util.Scanner;

public class OptionManage {

    Menu menu = new Menu();
    BooksList book = new BooksList();
    MoviesList movie = new MoviesList();
    UserList userList = new UserList();

    public void optionProcess(){
        System.out.println("Please input your library number");
        String userNum = getOption();
        System.out.println("Please input your password");
        String password = getOption();

        if(userList.isUserValid(userNum,password)) {
            String userInput;
            while (true) {
                System.out.println("Please choose your option");
                userInput = this.getOption();
                if (menu.mainMenu.get(0).equals(userInput))
                    book.printBooksList();
                else if ("Quit".equals(userInput)) ;
                else if (menu.mainMenu.get(1).equals(userInput))
                    this.checkoutBook();
                else if (menu.mainMenu.get(2).equals(userInput))
                    this.returnBook();
                else if (menu.mainMenu.get(3).equals(userInput))
                    movie.printMoviesList();
                else if (menu.mainMenu.get(4).equals(userInput))
                    this.checkoutMovies();
                else if (menu.mainMenu.get(5).equals(userInput))
                    this.returnMovies();
                else if(menu.mainMenu.get(6).equals(userInput))
                    userList.printUserInfo(userNum);
                else
                    System.out.println("Select a valid option");
            }
        }
        else {
            System.out.println("Invalid user, please confirm your input.");
            optionProcess();
        }
    }

    private String getOption() {
        String enterMessage;
        Scanner scan = new Scanner(System.in);
        enterMessage = scan.nextLine();
        return enterMessage;
    }

    private void checkoutBook() {
        String userInput;
        while (true) {
            System.out.println("Which one do you want to checkout?");
            userInput = this.getOption();
            if (userInput.equals("Quit"))
                break;
            if (checkoutBookOption(userInput))
                System.out.println("Thank you! Enjoy the book.");
            else
                System.out.println("That book is not available, please choose another book");
        }
    }

    private void returnBook() {
        String userInput;
        while (true) {
            System.out.println("Which one do you want to return?");
            userInput = this.getOption();
            if (userInput.equals("Quit"))
                break;
            if (returnBookOption(userInput))
                System.out.println("Thank you for returning the book.");
            else
                System.out.println("That is not a valid book to return, please confirm your input.");
        }
    }

    private boolean checkoutBookOption(String userInput){
        for(int i = 0; i<book.booksList.size();i++)
            if((book.booksList.get(i).getName().equals(userInput))&& book.booksList.get(i).isAvailable()){
                book.booksList.get(i).setAvailable(false);
                return true;
            }
        return  false;
    }

    private boolean returnBookOption(String userInput){
        for(int i = 0; i<book.booksList.size();i++)
            if((book.booksList.get(i).getName().equals(userInput))&& !book.booksList.get(i).isAvailable()){
                book.booksList.get(i).setAvailable(true);
                return true;
            }
        return  false;
    }
    private void checkoutMovies() {
        String userInput;
        while (true) {
            System.out.println("Which one do you want to checkout?");
            userInput = this.getOption();
            if (userInput.equals("Quit"))
                break;
            if (checkoutMoviesOption(userInput))
                System.out.println("Thank you! Enjoy the movie.");
            else
                System.out.println("That movie is not available, please choose another movie.");
        }
    }

    private void returnMovies() {
        String userInput;
        while (true) {
            System.out.println("Which one do you want to return?");
            userInput = this.getOption();
            if (userInput.equals("Quit"))
                break;
            if (returnMoviesOption(userInput))
                System.out.println("Thank you for returning the book.");
            else
                System.out.println("That is not a valid movie to return, please confirm your input. ");
        }
    }

    private boolean checkoutMoviesOption(String userInput){
        for(int i = 0; i<movie.moviesList.size();i++)
            if((movie.moviesList.get(i).getName().equals(userInput))&& movie.moviesList.get(i).isAvailable()){
                movie.moviesList.get(i).setAvailable(false);
                return true;
            }
        return false;
    }

    private boolean returnMoviesOption(String userInput){
        for(int i = 0; i<movie.moviesList.size();i++)
            if((movie.moviesList.get(i).getName().equals(userInput))&& !movie.moviesList.get(i).isAvailable()){
                movie.moviesList.get(i).setAvailable(true);
                return  true;
        }
        return false;
    }

}
